﻿using IOC.Desgin;
using IOC.Util;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace IOC.Builder
{
   public class MutiRegisterEntity
    {
		public List<RegisterEntity> RegisterEntityList { get; set; }

		public Type BaseType { get; set; }

		public MutiRegisterEntity(Assembly[] AssemblyList, Type BaseType, bool IsAttribute = false)
		{
			RegisterEntityList = new List<RegisterEntity>();
			this.BaseType = BaseType;
			foreach (Assembly assItem in AssemblyList)
			{
				Type[] typelist = null;
				typelist = ((!IsAttribute) ? (from a in assItem.GetTypes()
											  where CompareUtil.IsAssignableFrom(a, BaseType) && a != BaseType
											  select a).ToArray() : (from a in assItem.GetTypes()
																	 where a.GetCustomAttribute(BaseType, inherit: true) != null
																	 select a).ToArray());
				Type[] array = typelist;
				foreach (Type typeItem in array)
				{
					RegisterEntity RE = new RegisterEntity(typeItem);
					RegisterEntityList.Add(RE);
				}
			}
		}

		public MutiRegisterEntity AsImplementedInterfaces()
		{
			foreach (RegisterEntity item in RegisterEntityList)
			{
				item.AsImplementedInterfaces();
			}
			return this;
		}

		public MutiRegisterEntity EnableInterfaceInterceptors()
		{
			foreach (RegisterEntity item in RegisterEntityList)
			{
				item.EnableInterfaceInterceptors();
			}
			return this;
		}

		public MutiRegisterEntity InterceptedBy<T>() where T : IIntercept
		{
			foreach (RegisterEntity item in RegisterEntityList)
			{
				item.InterceptedBy<T>();
			}
			return this;
		}

		public MutiRegisterEntity UseAttributeIntercept()
		{
			foreach (RegisterEntity item in RegisterEntityList)
			{
				item.UseAttributeIntercept();
			}
			return this;
		}
	}
}
